package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //旋转矩阵 2
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int l = 0, r = n - 1, t = 0, b = n - 1, cnt = 0;
        int N = n * n;
        while (cnt < N) {
            // L->R T++;
            for (int i = l; i <= r; i++) {
                res[t][i] = ++cnt;
            }
            t++;
            // T->B R--;
            for (int i = t; i <= b; i++) {
                res[i][r] = ++cnt;
            }
            r--;
            // R->L B--;
            for (int i = r; i >= l; i--) {
                res[b][i] = ++cnt;
            }
            b--;
            // B->T L++;
            for (int i = b; i >= t; i--) {
                res[i][l] = ++cnt;
            }
            l++;
        }
        return res;
    }
}
